//
//  AppDelegate.m
//  彩票项目
//
//  Created by 伍银荣 on 2021/7/20.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "AppDelegate.h"
#import "WYRTabBarController.h"
#import "guideViewController.h"



@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //设置window对象的frame
    _window = [[UIWindow alloc]initWithFrame:kScreenSize];
    
    //创建tabBarController
    //WYRTabBarController *tabbarController = ;
    
    //设置window的根控制器
    _window.rootViewController = [self pickViewController];
    
    //设置window可见
    [_window makeKeyAndVisible];
    
    //把当前版本保存到沙盒中
    [self saveAppVersion];
    
    return YES;
}

- (UIViewController *)pickViewController {
    //判断"沙盒版本号"和"当前程序版本号(info)"是否一致
    if (![[self laodSaveAppVersion] isEqualToString:[self loadAppVersion]]) {
        //如果一致,就是tabbarController界面
       // NSLog(@"进入tabbarController界面");
        return [[WYRTabBarController alloc]init];
    } else {
        //如果不一致,就是引导页界面
       // NSLog(@"进入引导 页界面");
        GuideViewController *guideVC = [[GuideViewController alloc]init];
        //vc.view.backgroundColor = [UIColor yellowColor];
        return guideVC;
    }
}

//获取当前程序版本号
- (NSString *)loadAppVersion {
    //获取info.plist
    NSDictionary *info = [NSBundle mainBundle].infoDictionary;
    NSLog(@"***version = %@",info[@"CFBundleShortVersionString"]);
    //获取版本号
    return info[@"CFBundleShortVersionString"];
}

//获取沙盒中的版本号
- (NSString *)laodSaveAppVersion {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSLog(@"----appVersion = %@",[userDefaults objectForKey:@"appVersion"]);
    return [userDefaults objectForKey:@"appVersion"];
}

//将系统的版本号存储到沙盒中(沙盒版本号)
- (void)saveAppVersion {
    //获取info.plist
    NSDictionary *info = [NSBundle mainBundle].infoDictionary;
    //获取版本号
    NSString *appVersion = info[@"CFBundleShortVersionString"];
    NSLog(@"appVersion = %@",appVersion);
    //获取单例
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //将版本号存入沙盒中
    [userDefaults setObject:appVersion forKey:@"appVersion"];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
